#import "YYText.h"
#import "GKCover.h"
#import "dfhgwhruebvhrheycontaminategeneralize.h"
#import "dfhgwhruebvhrheyspotfaint.h"
#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheyfantasyviolinViewController.h"
#import "dfhgwhruebvhrheyaluminiumdisorder.h"
#import "dfhgwhruebvhrheyassemblepursuitViewController.h"
@interface dfhgwhruebvhrheyfantasyviolinViewController ()
@property (nonatomic, assign) NSInteger opsIdex;
@property (weak, nonatomic) IBOutlet UIButton *addBtn;
@property (weak, nonatomic) IBOutlet UIButton *reduceBtn;
@property (weak, nonatomic) IBOutlet dfhgwhruebvhrheycontaminategeneralize *loanView;
@property (weak, nonatomic) IBOutlet UIView *opsTermView;
@property (weak, nonatomic) IBOutlet UIView *opsAmountView;
@property (weak, nonatomic) IBOutlet YYLabel *tipsTextLabel;
@property (weak, nonatomic) IBOutlet UILabel *maxAmountTextLabel;
@property (weak, nonatomic) IBOutlet UILabel *currentAmountTextLabel;
@property (weak, nonatomic) IBOutlet UILabel *termTipsText;
@property (nonatomic, strong) dhgfywegwygeydwfurioussenate *seletedTerm;
@property (nonatomic, strong) dhgfywegwygeydwsenateassemble *selectedAmount;
@property (nonatomic, strong) NSMutableArray<dhgfywegwygeydwfurioussenate *> *terms;
@property (nonatomic, strong) NSMutableArray <dhgfywegwygeydwsenateassemble *> *amounts;
@end
@implementation dfhgwhruebvhrheyfantasyviolinViewController
- (void)loadUI {
	self.terms = [@[] mutableCopy];
	self.amounts = [@[] mutableCopy];
	self.navigationItem.title = @"Loan";
    [self configRightIcon:@"navigation_right"];
    [self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
	self.navigationStyle = dhgfywegwygeydwreciprocaltransport;
	self.loanView.vlineToTopHeight = 146;
	self.loanView.php_w = dhgfywegwygeydwdispositionvisualize - 26;
	[self.loanView drawArcAndLine:nil];
	self.opsAmountView.layer.cornerRadius = 20;
	self.opsAmountView.layer.masksToBounds = YES;
	self.opsAmountView.layer.maskedCorners = kCALayerMinXMinYCorner | kCALayerMaxXMinYCorner;
	NSString *tipsString = @"Warm tips:\n1. You can choose the required amount and loan date according to your needs.\n2. Repay on time, you can get a higher loan amount and a longer loan term next time.";
	NSMutableAttributedString *tipsAttribute = [[NSMutableAttributedString alloc] initWithString:tipsString];
	NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
	style.lineSpacing = 5;
	tipsAttribute.yy_paragraphStyle = style;
	tipsAttribute.yy_font = [UIFont regular:14];
	tipsAttribute.yy_color = [UIColor whiteColor];
	self.tipsTextLabel.numberOfLines = 0;
	self.tipsTextLabel.attributedText = tipsAttribute;
}
- (void)loadData {
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:loanInstalmentDataApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheyspotfaint *model = [dfhgwhruebvhrheyspotfaint yy_modelWithJSON:json];
		[[NSUserDefaults standardUserDefaults] setInteger:model.time forKey:dhgfywegwygeydwdiagramregardless];
		if (model.code == 200) {
			[strongSelf.terms removeAllObjects];
			if (model.user_data.count) {
				model.user_data.lastObject.seleted = YES;
				[strongSelf.terms addObjectsFromArray:model.user_data];
				[strongSelf bindTermsView];
			}
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)viewDidLoad {
    [super viewDidLoad];
	[self loadUI];
	[self loadData];
}
- (void)bindTermsView {
	dhgfywegwygeydwfurioussenate *term1 = [[dhgfywegwygeydwfurioussenate alloc] init];
	term1.ud_id = @"-100";
	term1.show_days = @"90";
	dhgfywegwygeydwfurioussenate *term2 = [[dhgfywegwygeydwfurioussenate alloc] init];
	term2.ud_id = @"-100";
	term2.show_days = @"60";
	dhgfywegwygeydwfurioussenate *term3 = [[dhgfywegwygeydwfurioussenate alloc] init];
	term3.ud_id = @"-100";
	term3.show_days = @"120";
	[self.terms addObject:term1];
	[self.terms addObject:term2];
	[self.terms addObject:term3];
	CGFloat space = 25;
	CGFloat lineSpace = 15;
	CGFloat termHeight = 36;
	CGFloat termWidth = (dhgfywegwygeydwdispositionvisualize - 28*2 - space*2)/3;
	for (dhgfywegwygeydwfurioussenate *term in self.terms) {
		NSInteger count = self.opsTermView.subviews.count;
		UIButton *termBtn = [UIButton buttonWithType:UIButtonTypeCustom];
		termBtn.layer.cornerRadius = 10;
		termBtn.tag = 100 + count;
		[termBtn setTitle:term.show_days forState:UIControlStateNormal];
		[termBtn setTitleColor:@"#3B414B".dhgfywegwygeydwlearneddesignate forState:UIControlStateNormal];
		[termBtn setTitleColor:primary_color.dhgfywegwygeydwlearneddesignate forState:UIControlStateSelected];
		[termBtn addTarget:self action:@selector(termBtnClick:) forControlEvents:UIControlEventTouchUpInside];
		[self.opsTermView addSubview:termBtn];
		if ([term.ud_id isEqualToString:@"-100"]) {
			termBtn.backgroundColor = @"#F6F6F6".dhgfywegwygeydwlearneddesignate;
			[termBtn setTitleColor:@"#AAAAAA".dhgfywegwygeydwlearneddesignate forState:UIControlStateNormal];
		}else {
			if (term.seleted) {
				termBtn.selected = YES;
				self.seletedTerm = term;
				termBtn.layer.borderWidth = 0;
				termBtn.backgroundColor = second_color.dhgfywegwygeydwlearneddesignate;
                if (term.info.instalment_num.intValue == 1) {
                    self.termTipsText.text = @"Only accepts full repayment";
                }else {
                    self.termTipsText.text = [NSString stringWithFormat:@"Divided into %@ installments to repay", term.info.instalment_num];
                }
			}else {
				termBtn.selected = NO;
				termBtn.layer.borderWidth = 0.5;
				termBtn.layer.borderColor = @"#CCCCCC".dhgfywegwygeydwlearneddesignate.CGColor;
			}
		}
		[termBtn mas_makeConstraints:^(MASConstraintMaker *make) {
			make.size.mas_equalTo(CGSizeMake(termWidth, termHeight));
			make.left.equalTo(self.opsTermView).offset((count % 3) * (termWidth + space));
			make.top.equalTo(self.opsTermView).offset((count / 3) * (termHeight + lineSpace));
		}];
	}
	[self bindAmountView];
}
- (void)termBtnClick:(UIButton *)sender {
	dhgfywegwygeydwfurioussenate *term = self.terms[sender.tag - 100];
	if ([term.ud_id isEqualToString:@"-100"]) {
		return [dfhgwhruebvhrheyoptionsaving.keyWindow show:@"Repay on time for a longer loan term"];
	}
	for (UIButton *termBtn in sender.superview.subviews) {
		dhgfywegwygeydwfurioussenate *term = self.terms[termBtn.tag - 100];
		if (![term.ud_id isEqualToString:@"-100"]) {
			term.seleted = NO;
			termBtn.selected = NO;
			termBtn.layer.borderWidth = 0.5;
			termBtn.backgroundColor = [UIColor whiteColor];
			termBtn.layer.borderColor = @"#CCCCCC".dhgfywegwygeydwlearneddesignate.CGColor;
		}
	}
	sender.selected = YES;
	sender.layer.borderWidth = 0;
	sender.backgroundColor = second_color.dhgfywegwygeydwlearneddesignate;
	term.seleted = YES;
	self.seletedTerm = term;
	[self bindAmountView];
	if ([term.info.instalment_num intValue] == 1) {
		self.termTipsText.text = @"Only accepts full repayment";
	}else {
		self.termTipsText.text = [NSString stringWithFormat:@"Divided into %@ installments to repay", term.info.instalment_num];
	}
}
- (void)bindAmountView {
	[self.amounts removeAllObjects];
	for (dhgfywegwygeydwsenateassemble *amount in self.seletedTerm.info.data) {
		if ([amount.amount intValue] <= [self.seletedTerm.info.money intValue]) {
			[self.amounts addObject:amount];
		}
	}
	if (self.amounts.count) {
		self.reduceBtn.enabled = YES;
		self.reduceBtn.selected = NO;
		self.reduceBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
		self.addBtn.enabled = NO;
		self.addBtn.selected = YES;
		self.selectedAmount = self.amounts.lastObject;
		self.addBtn.backgroundColor = @"#CCCCCC".dhgfywegwygeydwlearneddesignate;
	}
	self.opsIdex = self.amounts.count - 1;
	self.currentAmountTextLabel.text = self.selectedAmount.amount.dhgfywegwygeydwlocalitylearned;
	self.maxAmountTextLabel.text = [NSString stringWithFormat:@"PHP %@",self.selectedAmount.amount.dhgfywegwygeydwlocalitylearned];
}
- (IBAction)opsBtnClick:(UIButton *)sender {
	switch (sender.tag) {
		case 100: {
			NSLog(@"金额减");
			self.opsIdex --;
			if (self.opsIdex <= 0) {
				self.opsIdex = 0;
				self.reduceBtn.enabled = NO;
				self.reduceBtn.selected = YES;
				self.selectedAmount = self.amounts.firstObject;
				self.reduceBtn.backgroundColor = @"#CCCCCC".dhgfywegwygeydwlearneddesignate;
			}else {
				self.reduceBtn.enabled = YES;
				self.reduceBtn.selected = NO;
				self.selectedAmount = self.amounts[self.opsIdex];
				self.reduceBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
			}
			self.addBtn.selected = NO;
			self.addBtn.enabled = YES;
			self.addBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
			self.currentAmountTextLabel.text = self.selectedAmount.amount.dhgfywegwygeydwlocalitylearned;
		}
			break;
		case 200: {
			NSLog(@"金额加");
			self.opsIdex ++;
			if (self.opsIdex >= self.amounts.count - 1) {
				self.addBtn.enabled = NO;
				self.addBtn.selected = YES;
				self.opsIdex = self.amounts.count - 1;
				self.selectedAmount = self.amounts.lastObject;
				self.addBtn.backgroundColor = @"#CCCCCC".dhgfywegwygeydwlearneddesignate;
			}else {
				self.addBtn.selected = NO;
				self.addBtn.enabled = YES;
				self.selectedAmount = self.amounts[self.opsIdex];
				self.addBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
			}
			self.reduceBtn.enabled = YES;
			self.reduceBtn.selected = NO;
			self.reduceBtn.backgroundColor = primary_color.dhgfywegwygeydwlearneddesignate;
			self.currentAmountTextLabel.text = self.selectedAmount.amount.dhgfywegwygeydwlocalitylearned;
		}
			break;
		case 300: {
			dfhgwhruebvhrheyaluminiumdisorder *popView = [[dfhgwhruebvhrheyaluminiumdisorder alloc] initWithFrame:CGRectMake(38, 0, dhgfywegwygeydwdispositionvisualize-38*2, 401)];
			[GKCover coverFrom:[dfhgwhruebvhrheyoptionsaving keyWindow] contentView:popView style:GKCoverStyleTranslucent showStyle:GKCoverShowStyleCenter showAnimStyle:GKCoverShowAnimStyleCenter hideAnimStyle:GKCoverHideAnimStyleCenter notClick:YES];
			__weak typeof(self) weakSelf = self;
			[popView configure:@"Warm Reminder" andTips:@"During the audit exemption period, the privilege of exempt audit and direct loan is within 210 days at most with a good repayment habit.  If you do not pay within the promised date, PHPcketPro will have the right to consult and use your personal contact information, including FB, Address Book, Twitter, Instagram, Google+, etc." withBtnsClick:^(UIButton * _Nonnull sender) {
				__strong typeof(weakSelf) strongSelf = weakSelf;
				[GKCover hideCover];
				if(sender.tag == 101) {
					[strongSelf loanAction];
				}
			}];
		}
			break;
		default:
			break;
	}
}
- (void)loanAction {
	dfhgwhruebvhrheyassemblepursuitViewController *planVC = [[dfhgwhruebvhrheyassemblepursuitViewController alloc] init];
	planVC.seletedTerm = self.seletedTerm;
	planVC.selectedAmount = self.selectedAmount;
	[self.navigationController pushViewController:planVC animated:YES];
}
@end
